package leetcode.editor.week.week308;

// 2391. 收集垃圾的最少总时间
// https://leetcode.cn/problems/minimum-amount-of-time-to-collect-garbage/
public class Solution2391 {
    public static void main(String[] args) {
        Solution2391 solution6162 = new Solution2391();
        // solution6162.garbageCollection(new String[]{"G","P","GP","GG"}, new int[]{2, 4, 3});
        solution6162.garbageCollection(new String[]{"G","G"}, new int[]{1});
    }

    public int garbageCollection(String[] garbage, int[] travel) {
        int ml = 0, gl = 0, pl = 0, mc = 0, gc = 0, pc = 0;
        for(int i = 0; i < garbage.length; i++) {
            String s = garbage[i];
            for(int j = 0; j < s.length(); j++) {
                if(s.charAt(j) == 'M') {
                    mc++;
                    ml = i;
                }else if(s.charAt(j) == 'G') {
                    gc++;
                    gl = i;
                } else {
                    pc++;
                    pl = i;
                }
            }
        }

        for(int i = 1; i < travel.length; i++) {
            travel[i] += travel[i - 1];
        }

        int ans = mc + gc + pc;
        if(ml >= 1) {
            ans += travel[ml - 1];
        }

        if(pl >= 1) {
            ans += travel[pl - 1];
        }

        if(gl >= 1) {
            ans += travel[gl - 1];
        }

        return ans;
    }
}
